//
//  NSObject+RandomData.m
//  HDCommon
//
//  Created by zhd on 2023/6/20.
//

#import "NSObject+RandomData.h"
#import <objc/runtime.h>

@implementation NSObject (RandomData)
//只支持有限的类型，不支持nsdictionary。
+(instancetype) randomData{
    
    
    unsigned int count;
    
    id randomModel = [[[self class] alloc] init];
    
    Ivar * ivarList = class_copyIvarList([self class], &count);
    
    for (unsigned int i = 0; i < count; i ++) {
        
        Ivar myIvar = ivarList[i];
        
        const char * ivarName = ivar_getName(myIvar);
        const char * typeEncoding = ivar_getTypeEncoding(myIvar);
        
        NSString * ivarNameStr = [NSString stringWithUTF8String:ivarName];
        NSString * typeEncodingStr = [NSString stringWithUTF8String:typeEncoding];
        NSLog(@"%@",ivarNameStr);
        ivarNameStr = [ivarNameStr substringFromIndex:1];
        if ([typeEncodingStr isEqualToString:@"@\"NSString\""]) {
            [randomModel setValue:[self randomString] forKey:ivarNameStr];
        }else if([typeEncodingStr isEqualToString:@"B"]){// bool
            [randomModel setValue:@([self randomBool]) forKey:ivarNameStr];
        }else if([typeEncodingStr isEqualToString:@"q"]){
            [randomModel setValue:@([self randomInteger]) forKey:ivarNameStr];
        }else if([typeEncodingStr isEqualToString:@"d"]){
            [randomModel setValue:@([self randomFloat]) forKey:ivarNameStr];
        }else if([typeEncodingStr isEqualToString:@"@\"NSArray\""]){
            
            NSMutableArray * mArray = [NSMutableArray array];
            
            NSMutableArray * typeArray = [NSMutableArray array];
            
            if ([self respondsToSelector:@selector(arrayType)]) {
                NSDictionary * dict = [self performSelector:@selector(arrayType)];
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    id types = dict[ivarNameStr];
                    
                    if ([types isKindOfClass:[NSArray class]]) {
                        typeArray = types;
                    }else if([types isKindOfClass:[NSString class]]){
                        [typeArray addObject:types];
                    }
                    
                }
                
            }
            
            NSInteger makeEleNum = random() % 20;
            
            
            for (NSInteger i = 0; i < makeEleNum; i += 1) {
                
                NSInteger eleTypeIndex = random() % typeArray.count;
                
                
                
                if ([typeArray[eleTypeIndex] isEqualToString:@"int"]) {
                    [mArray addObject:@([self randomInteger])];
                }else if([typeArray[eleTypeIndex] isEqualToString:@"float"]){
                    [mArray addObject:@([self randomFloat])];
                }else if([typeArray[eleTypeIndex] isEqualToString:@"bool"]){
                    [mArray addObject:@([self randomBool])];
                }else{
                    
                    Class clz = NSClassFromString(typeArray[eleTypeIndex]);
                    
                    
                    if ([[clz new] isKindOfClass:[NSString class]]) {
                        [mArray addObject:[self randomString]];
                    }else{
                        [mArray addObject:[clz randomData]];
                    }
                    
                }
                
            }
            
            
            [randomModel setValue:mArray forKey:ivarNameStr];
            
            
            
            
            
            
            
        }else{
            
            if (typeEncodingStr.length > 3) {
                NSString * classTypeStr = [typeEncodingStr substringToIndex: typeEncodingStr.length - 1];
                classTypeStr = [classTypeStr substringFromIndex:2];
                Class randomClass = NSClassFromString(classTypeStr);
                
                [randomModel setValue:[randomClass randomData] forKey:ivarNameStr];
            }
            

        }
        
        NSLog(@" ivarName: %@, type %@",ivarNameStr,typeEncodingStr);
        
        
    }
    
    return randomModel;
}

+(CGFloat) randomFloat{
    NSInteger value = random() % 10000000;
    
    CGFloat xiaoshu = (CGFloat)(value % 100);
                    
    return xiaoshu / 100 + (CGFloat)value / 100;
    
    
}


+(NSInteger) randomInteger{
    return random() % 10000000;
}

+(BOOL) randomBool{
    return random() % 2 == 1;
}
+(NSString *) randomString{
    NSInteger length = (NSInteger)random() % 20;
    if (length == 0) {
        length = 1;
    }
    NSString * str = @"10 夜雨与风雪》笔记4 一张便条 1.白0987654321居易的《问刘十九》: 绿蚁新醅酒，红泥小火炉。晚来天欲雪，能饮一杯无？ “绿蚁新醅酒”，古代酿造的新abcdefghijklmnopqrstuvwxyz1234567890";
    
    NSMutableString * mStr = [NSMutableString string];
    
    for (NSInteger i = 0; i < length; i += 1) {
        NSString * subString = [str substringWithRange:(NSMakeRange((random() % str.length), 1))];
        [mStr appendString:subString];
    }
    
    
    
    return  mStr;
}
@end
